package cg.object.builder.dialogs;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.internal.corext.codemanipulation.tostringgeneration.ToStringGenerationSettings.CustomBuilderSettings;
import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil;
import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
import org.eclipse.jdt.internal.corext.util.Messages;
import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.JavaUIMessages;
import org.eclipse.jdt.internal.ui.dialogs.SourceActionDialog;
import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
import org.eclipse.jdt.internal.ui.viewsupport.BindingLabelProvider;
import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
import org.eclipse.jdt.ui.JavaElementImageDescriptor;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.dialogs.ISelectionStatusValidator;

import cg.object.builder.i18n.FluentBuilderMessages;

/**
 * Dialog for the generate Fluent Object Builder action.
 * 
 * @author roucouj@gmail.com
 */
public class GenerateFluentBuilderDialog extends SourceActionDialog {

	private static class GenerateFluentBuilderContentProvider implements ITreeContentProvider {

		private static final Object[] EMPTY = new Object[0];

		private Object[] fFields;

		private Object[] fMethods;

		private Object[] fInheritedFields;

		private Object[] fInheritedMethods;

		private Object[] mainNodes;

		private static final String fieldsNode = JavaUIMessages.GenerateToStringDialog_fields_node;

		private static final String methodsNode = JavaUIMessages.GenerateToStringDialog_methods_node;

		private static final String inheritedFieldsNode = JavaUIMessages.GenerateToStringDialog_inherited_fields_node;

		private static final String inheritedMethodsNode = JavaUIMessages.GenerateToStringDialog_inherited_methods_node;

		public GenerateFluentBuilderContentProvider(IVariableBinding[] fields, IVariableBinding[] inheritedFields,
				IMethodBinding[] methods, IMethodBinding[] inheritedMethods) {
			ArrayList nodes = new ArrayList();
			fFields = (Object[]) fields.clone();
			if (fFields.length > 0)
				nodes.add(fieldsNode);
			fInheritedFields = (Object[]) inheritedFields.clone();
			if (fInheritedFields.length > 0)
				nodes.add(inheritedFieldsNode);
			fMethods = (Object[]) methods.clone();
			if (fMethods.length > 0)
				nodes.add(methodsNode);
			fInheritedMethods = (Object[]) inheritedMethods.clone();
			if (fInheritedMethods.length > 0)
				nodes.add(inheritedMethodsNode);
			mainNodes = nodes.toArray();
		}

		private int getElementPosition(Object element, Object[] array) {
			for (int i = 0; i < array.length; i++) {
				if (array[i].equals(element)) {
					return i;
				}
			}
			return -1;
		}

		private Object[] getContainingArray(Object element) {
			if (element instanceof String)
				return mainNodes;
			if (element instanceof IVariableBinding) {
				if (getElementPosition(element, fFields) >= 0)
					return fFields;
				if (getElementPosition(element, fInheritedFields) >= 0)
					return fInheritedFields;
			}
			if (element instanceof IMethodBinding) {
				if (getElementPosition(element, fMethods) >= 0)
					return fMethods;
				if (getElementPosition(element, fInheritedMethods) >= 0)
					return fInheritedMethods;
			}
			return EMPTY;
		}

		// public boolean canMoveDown(Object element) {
		// Object[] array = getContainingArray(element);
		// int position = getElementPosition(element, array);
		// return position != -1 && position != array.length - 1;
		// }

		// public boolean canMoveUp(Object element) {
		// return getElementPosition(element, getContainingArray(element)) > 0;
		// }

		// public void down(Object element, CheckboxTreeViewer tree) {
		// move(element, tree, 1);
		// }

		// public void up(Object element, CheckboxTreeViewer tree) {
		// move(element, tree, -1);
		// }

		// private void move(Object element, CheckboxTreeViewer tree, int
		// direction) {
		// Object[] array = getContainingArray(element);
		// int position = getElementPosition(element, array);
		// Object temp = array[position];
		// array[position] = array[position + direction];
		// array[position + direction] = temp;
		// tree.setSelection(new StructuredSelection(element));
		// tree.refresh();
		// }

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang
		 * .Object)
		 */
		public Object[] getChildren(Object parentElement) {
			if (parentElement == fieldsNode)
				return fFields;
			if (parentElement == inheritedFieldsNode)
				return fInheritedFields;
			if (parentElement == methodsNode)
				return fMethods;
			if (parentElement == inheritedMethodsNode)
				return fInheritedMethods;
			return EMPTY;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang
		 * .Object)
		 */
		public Object getParent(Object element) {
			Object[] array = getContainingArray(element);
			if (array == fFields)
				return fieldsNode;
			if (array == fInheritedFields)
				return inheritedFieldsNode;
			if (array == fMethods)
				return methodsNode;
			if (array == fInheritedMethods)
				return inheritedMethodsNode;
			return null;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang
		 * .Object)
		 */
		public boolean hasChildren(Object element) {
			if (element instanceof String)
				return true;
			return false;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(
		 * java.lang.Object)
		 */
		public Object[] getElements(Object inputElement) {
			return mainNodes;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
		 */
		public void dispose() {
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse
		 * .jface.viewers.Viewer, java.lang.Object, java.lang.Object)
		 */
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}

	private static class GenerateFluentBuilderLabelProvider extends BindingLabelProvider {
		public Image getImage(Object element) {
			ImageDescriptor descriptor = null;
			if (element == GenerateFluentBuilderContentProvider.fieldsNode
					|| element == GenerateFluentBuilderContentProvider.inheritedFieldsNode)
				descriptor = JavaPluginImages.DESC_FIELD_PUBLIC;
			if (element == GenerateFluentBuilderContentProvider.methodsNode
					|| element == GenerateFluentBuilderContentProvider.inheritedMethodsNode)
				descriptor = JavaPluginImages.DESC_MISC_PUBLIC;
			if (descriptor != null) {
				descriptor = new JavaElementImageDescriptor(descriptor, 0, JavaElementImageProvider.BIG_SIZE);
				return JavaPlugin.getImageDescriptorRegistry().get(descriptor);
			}
			return super.getImage(element);
		}
	}

	private class GenerateFluentBuilderValidator implements ISelectionStatusValidator {

		private int fNumFields;

		private int fNumMethods;

		private CustomBuilderValidator fValidator;

		public GenerateFluentBuilderValidator(int fields, int methods) {
			fNumFields = fields;
			fNumMethods = methods;
		}

		public IStatus validate(Object[] selection) {
			// if (getGenerationSettings().toStringStyle ==
			// GenerateToStringOperation.CUSTOM_BUILDER) {
			// if (fValidator == null)
			// fValidator = new
			// CustomBuilderValidator(getType().getJavaProject());
			// IStatus status =
			// fValidator.revalidateAll(getGenerationSettings().getCustomBuilderSettings());
			// if (!status.isOK())
			// return new StatusInfo(IStatus.ERROR,
			// JavaUIMessages.GenerateToStringDialog_selectioninfo_customBuilderConfigError);
			// }

			int countFields = 0, countMethods = 0;
			for (int index = 0; index < selection.length; index++) {
				if (selection[index] instanceof IVariableBinding)
					countFields++;
				else if (selection[index] instanceof IMethodBinding)
					countMethods++;
			}

			return new StatusInfo(IStatus.INFO, Messages.format(
					JavaUIMessages.GenerateToStringDialog_selectioninfo_more,
					new String[] { String.valueOf(countFields), String.valueOf(fNumFields),
							String.valueOf(countMethods), String.valueOf(fNumMethods) }));
		}
	}

	private static class CustomBuilderValidator implements ISelectionStatusValidator {

		private final IJavaProject fJavaProject;

		private IType fLastValidBuilderType;

		private List fLastValidAppendMethodSuggestions;

		private List fLastValidResultMethodSuggestions;

		public CustomBuilderValidator(IJavaProject javaProject) {
			fJavaProject = javaProject;
		}

		public IStatus validateBuilderType(IType type) {
			if (fLastValidBuilderType != null && fLastValidBuilderType.equals(type)) {
				return new StatusInfo();
			}

			try {
				IMethod[] methods = type.getMethods();
				boolean foundConstructor = false;
				for (int i = 0; i < methods.length; i++) {
					if (methods[i].isConstructor() && Flags.isPublic(methods[i].getFlags())) {
						String[] parameterTypes = methods[i].getParameterTypes();
						if (parameterTypes.length == 1
								&& "java.lang.Object".equals(JavaModelUtil.getResolvedTypeName(parameterTypes[0], type))) { //$NON-NLS-1$
							foundConstructor = true;
							break;
						}
					}
				}
				if (!foundConstructor)
					return new StatusInfo(IStatus.ERROR,
							JavaUIMessages.GenerateToStringDialog_customBuilderConfig_noConstructorError);

				List appendMethodSuggestions = getAppendMethodSuggestions(type);
				if (appendMethodSuggestions.isEmpty())
					return new StatusInfo(IStatus.ERROR,
							JavaUIMessages.GenerateToStringDialog_customBuilderConfig_noAppendMethodError);

				List resultMethodSuggestions = getResultMethodSuggestions(type);
				if (resultMethodSuggestions.isEmpty())
					return new StatusInfo(IStatus.ERROR,
							JavaUIMessages.GenerateToStringDialog_customBuilderConfig_noResultMethodError);

				fLastValidBuilderType = type;
				fLastValidAppendMethodSuggestions = appendMethodSuggestions;
				fLastValidResultMethodSuggestions = resultMethodSuggestions;
				return new StatusInfo();
			} catch (JavaModelException e1) {
				return new StatusInfo(IStatus.WARNING,
						JavaUIMessages.GenerateToStringDialog_customBuilderConfig_typeValidationError);
			}
		}

		public IStatus validate(Object[] selection) {
			return validateBuilderType(((IType) selection[0]));
		}

		public IStatus revalidateAll(CustomBuilderSettings builderSettings) {
			try {
				if (builderSettings.className.length() == 0) {
					return new StatusInfo(IStatus.ERROR,
							JavaUIMessages.GenerateToStringDialog_customBuilderConfig_noBuilderClassError);
				}

				IType type = findType(builderSettings.className);

				if (type == null || !type.exists()) {
					return new StatusInfo(IStatus.ERROR, MessageFormat.format(
							JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidClassError,
							new String[] { builderSettings.className }));
				}

				IStatus typeValidation = validateBuilderType(type);
				if (!typeValidation.isOK())
					return typeValidation;

				if (!getAppendMethodSuggestions(type).contains(builderSettings.appendMethod))
					return new StatusInfo(IStatus.ERROR, MessageFormat.format(
							JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidAppendMethodError,
							new String[] { builderSettings.appendMethod }));

				if (!getResultMethodSuggestions(type).contains(builderSettings.resultMethod))
					return new StatusInfo(IStatus.ERROR, MessageFormat.format(
							JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidResultMethodError,
							new String[] { builderSettings.resultMethod }));

				if (!isValidJavaIdentifier(builderSettings.variableName))
					return new StatusInfo(IStatus.ERROR, MessageFormat.format(
							JavaUIMessages.GenerateToStringDialog_customBuilderConfig_invalidVariableNameError,
							new String[] { builderSettings.variableName }));

			} catch (JavaModelException e) {
				return new StatusInfo(IStatus.WARNING,
						JavaUIMessages.GenerateToStringDialog_customBuilderConfig_dataValidationError);
			}
			return new StatusInfo();
		}

		public IType findType(String builderClassName) throws JavaModelException {
			if (fLastValidBuilderType != null
					&& builderClassName.equals(fLastValidBuilderType.getFullyQualifiedParameterizedName())) {
				return fLastValidBuilderType;
			}

			return fJavaProject.findType(builderClassName, (IProgressMonitor) null);
		}

		public List getAppendMethodSuggestions(final IType type) throws JavaModelException {
			if (fLastValidBuilderType != null && fLastValidBuilderType.equals(type)) {
				return fLastValidAppendMethodSuggestions;
			}
			return getMethodSuggestions(type, new MethodChecker() {
				public boolean isMethodOK(IMethod method) throws JavaModelException {
					if (!Flags.isPublic(method.getFlags()) || method.isConstructor())
						return false;
					/*
					 * To be an append method, it must take exactly one Object
					 * parameter, and optionally one String parameter.
					 */
					String[] parameterTypes = method.getParameterTypes();
					if (parameterTypes.length == 0 || parameterTypes.length > 2) {
						return false;
					}
					int countObjects = 0, countStrings = 0;
					for (int i = 0; i < parameterTypes.length; i++) {
						String resolvedParameterTypeName = JavaModelUtil.getResolvedTypeName(parameterTypes[i], type);
						if ("java.lang.Object".equals(resolvedParameterTypeName))//$NON-NLS-1$
							countObjects++;
						if ("java.lang.String".equals(resolvedParameterTypeName))//$NON-NLS-1$
							countStrings++;
					}
					return countObjects == 1 && countObjects + countStrings == parameterTypes.length;

				}
			});
		}

		public List getResultMethodSuggestions(final IType type) throws JavaModelException {
			if (fLastValidBuilderType != null && fLastValidBuilderType.equals(type)) {
				return fLastValidResultMethodSuggestions;
			}
			return getMethodSuggestions(type, new MethodChecker() {
				public boolean isMethodOK(IMethod method) throws JavaModelException {
					return Flags.isPublic(method.getFlags())
							&& method.getParameterTypes().length == 0
							&& "java.lang.String".equals(JavaModelUtil.getResolvedTypeName(method.getReturnType(), type)); //$NON-NLS-1$
				}
			});
		}

		private interface MethodChecker {
			boolean isMethodOK(IMethod method) throws JavaModelException;
		}

		private List getMethodSuggestions(IType type, MethodChecker checker) throws JavaModelException {
			ArrayList result = new ArrayList();
			IType[] classes = type.newSupertypeHierarchy(null).getAllClasses();
			for (int i = 0; i < classes.length; i++) {
				IMethod[] methods = classes[i].getMethods();
				for (int j = 0; j < methods.length; j++) {
					if (checker.isMethodOK(methods[j])) {
						String name = methods[j].getElementName();
						if (!result.contains(name))
							result.add(name);
					}
				}
			}
			return result;
		}

		private boolean isValidJavaIdentifier(String identifier) {
			return JavaConventionsUtil.validateIdentifier(identifier, fJavaProject).isOK();
		}
	}

	// private ToStringGenerationSettings fGenerationSettings;

	// private static final int DOWN_BUTTON = IDialogConstants.CLIENT_ID + 2;

	// private static final int UP_BUTTON = IDialogConstants.CLIENT_ID + 1;

	protected Button[] fButtonControls;

	boolean[] fButtonsEnabled;

	// private static final int DOWN_INDEX = 1;

	// private static final int UP_INDEX = 0;

	// public ToStringGenerationSettings getGenerationSettings() {
	// return fGenerationSettings;
	// }

	// public static String[] getTemplates(IDialogSettings dialogSettings) {
	// String[] result =
	// dialogSettings.getArray(ToStringGenerationSettings.SETTINGS_TEMPLATES);
	// if (result != null && result.length > 0)
	// return result;
	// return new String[] { ToStringTemplateParser.DEFAULT_TEMPLATE };
	// }

	// public String[] getTemplateNames() {
	// String[] result =
	// getDialogSettings().getArray(ToStringGenerationSettings.SETTINGS_TEMPLATE_NAMES);
	// if (result != null && result.length > 0)
	// return result;
	// return new String[] {
	// JavaUIMessages.GenerateToStringDialog_defaultTemplateName };
	// }

	// public int getSelectedTemplate() {
	// try {
	// int result =
	// getDialogSettings().getInt(ToStringGenerationSettings.SETTINGS_SELECTED_TEMPLATE);
	// if (result < 0)
	// return 0;
	// return result;
	// } catch (NumberFormatException e) {
	// return 0;
	// }
	// }

	// public void setSelectedTemplate(int templateNumber) {
	// getDialogSettings().put(ToStringGenerationSettings.SETTINGS_SELECTED_TEMPLATE,
	// templateNumber);
	// }

	public GenerateFluentBuilderDialog(Shell shell, CompilationUnitEditor editor, IType type,
			IVariableBinding[] fields, IVariableBinding[] inheritedFields, IVariableBinding[] selectedFields,
			IMethodBinding[] methods, IMethodBinding[] inheritededMethods) throws JavaModelException {
		super(shell, new BindingLabelProvider(), new GenerateFluentBuilderContentProvider(fields, inheritedFields,
				methods, inheritededMethods), editor, type, false);
		setEmptyListMessage(JavaUIMessages.GenerateHashCodeEqualsDialog_no_entries);

		List selected = new ArrayList(Arrays.asList(selectedFields));
		if (selectedFields.length == fields.length && selectedFields.length > 0)
			selected.add(getContentProvider().getParent(selectedFields[0]));
		setInitialElementSelections(selected);

		setTitle(FluentBuilderMessages.GenerateFluentBuilderDialog_dialog_title);
		setMessage(FluentBuilderMessages.GenerateFluentBuilderDialog_select_fields_to_include);
		setValidator(new GenerateFluentBuilderValidator(fields.length + inheritedFields.length, methods.length
				+ inheritededMethods.length));
		setSize(60, 18);
		setInput(new Object());

		// fGenerationSettings = new
		// ToStringGenerationSettings(getDialogSettings());
	}

	/**
	 * {@inheritDoc}
	 */
	public boolean close() {
		// fGenerationSettings.writeDialogSettings();

		// fGenerationSettings.stringFormatTemplate =
		// getTemplates(getDialogSettings())[fGenerationSettings.stringFormatTemplateNumber];

		// fGenerationSettings.createComments = getGenerateComment();

		return super.close();
	}

	public Object[] getResult() {
		Object[] oldResult = super.getResult();
		List newResult = new ArrayList();
		for (int i = 0; i < oldResult.length; i++) {
			if (!(oldResult[i] instanceof String)) {
				newResult.add(oldResult[i]);
			}
		}
		return newResult.toArray();
	}

	protected void configureShell(Shell shell) {
		super.configureShell(shell);
		PlatformUI.getWorkbench().getHelpSystem()
				.setHelp(shell, IJavaHelpContextIds.GENERATE_TOSTRING_SELECTION_DIALOG);
	}

	protected CheckboxTreeViewer createTreeViewer(Composite parent) {
		CheckboxTreeViewer treeViewer = super.createTreeViewer(parent);

		treeViewer.setLabelProvider(new GenerateFluentBuilderLabelProvider());

		// expandAll because setSubtreeChecked() used in CheckStateListener
		// below assumes that elements have been expanded
		treeViewer.expandAll();
		// but actually we only need one branch expanded
		treeViewer.collapseAll();
		treeViewer.expandToLevel(GenerateFluentBuilderContentProvider.fieldsNode, 1);

		treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection selection = (IStructuredSelection) getTreeViewer().getSelection();

				Object selected = selection.size() > 0 ? selection.toList().get(0) : null;
				GenerateFluentBuilderContentProvider cp = (GenerateFluentBuilderContentProvider) getContentProvider();

				// fButtonControls[UP_INDEX].setEnabled(cp.canMoveUp(selected));
				// fButtonControls[DOWN_INDEX].setEnabled(cp.canMoveDown(selected));
			}

		});
		treeViewer.addCheckStateListener(new ICheckStateListener() {
			public void checkStateChanged(CheckStateChangedEvent event) {
				getTreeViewer().setSubtreeChecked(event.getElement(), event.getChecked());
				getTreeViewer().setGrayed(event.getElement(), false);
				Object parentElement = ((ITreeContentProvider) (getTreeViewer().getContentProvider())).getParent(event
						.getElement());
				if (parentElement != null) {
					Object[] siblings = ((ITreeContentProvider) (getTreeViewer().getContentProvider()))
							.getChildren(parentElement);
					int count = 0;
					for (int i = 0; i < siblings.length; i++) {
						if (getTreeViewer().getChecked(siblings[i]))
							count++;
					}
					if (count == 0)
						getTreeViewer().setGrayChecked(parentElement, false);
					else if (count == siblings.length) {
						getTreeViewer().setChecked(parentElement, true);
						getTreeViewer().setGrayed(parentElement, false);
					} else
						getTreeViewer().setGrayChecked(parentElement, true);
				}
				updateOKStatus();
			}

		});
		return treeViewer;
	}

	protected Composite createSelectionButtons(Composite composite) {
		Composite buttonComposite = super.createSelectionButtons(composite);

		GridLayout layout = new GridLayout();
		buttonComposite.setLayout(layout);

		// createUpDownButtons(buttonComposite);

		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.numColumns = 1;

		return buttonComposite;
	}

	// protected void buttonPressed(int buttonId) {
	// super.buttonPressed(buttonId);
	// switch (buttonId) {
	// case UP_BUTTON: {
	// GenerateFluentBuilderContentProvider contentProvider =
	// (GenerateFluentBuilderContentProvider) getTreeViewer()
	// .getContentProvider();
	// List selection = ((IStructuredSelection)
	// getTreeViewer().getSelection()).toList();
	// if (selection.size() > 0)
	// contentProvider.up(selection.get(0), getTreeViewer());
	// updateOKStatus();
	// break;
	// }
	// case DOWN_BUTTON: {
	// GenerateFluentBuilderContentProvider contentProvider =
	// (GenerateFluentBuilderContentProvider) getTreeViewer()
	// .getContentProvider();
	// List selection = ((IStructuredSelection)
	// getTreeViewer().getSelection()).toList();
	// if (selection.size() > 0)
	// contentProvider.down(selection.get(0), getTreeViewer());
	// updateOKStatus();
	// break;
	// }
	// }
	// }

	// protected void createUpDownButtons(Composite buttonComposite) {
	// int numButtons = 2; // up, down
	// fButtonControls = new Button[numButtons];
	// fButtonsEnabled = new boolean[numButtons];
	// fButtonControls[UP_INDEX] = createButton(buttonComposite, UP_BUTTON,
	// JavaUIMessages.GenerateToStringDialog_up_button, false);
	// fButtonControls[DOWN_INDEX] = createButton(buttonComposite, DOWN_BUTTON,
	// JavaUIMessages.GenerateToStringDialog_down_button, false);
	// boolean defaultState = false;
	// fButtonControls[UP_INDEX].setEnabled(defaultState);
	// fButtonControls[DOWN_INDEX].setEnabled(defaultState);
	// fButtonsEnabled[UP_INDEX] = defaultState;
	// fButtonsEnabled[DOWN_INDEX] = defaultState;
	// }

	private Label formatLabel;

	private Combo formatCombo;

	private Button skipNullsButton;

	private Button styleButton;

	protected Composite createCommentSelection(Composite parentComposite) {
		Composite composite = super.createCommentSelection(parentComposite);

		// Group group = new Group(parentComposite, SWT.NONE);
		// group.setText(JavaUIMessages.GenerateToStringDialog_generated_code_group);
		// GridLayout groupLayout = new GridLayout();
		// group.setLayout(groupLayout);
		// group.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
		//
		// Composite composite2 = new Composite(group, SWT.NONE);
		// GridLayout layout = new GridLayout(3, false);
		// layout.marginWidth = 0;
		// layout.marginHeight = 0;
		// composite2.setLayout(layout);
		// composite2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
		// false));
		//
		// formatLabel = new Label(composite2, SWT.NONE);
		// formatLabel.setText(JavaUIMessages.GenerateToStringDialog_string_format_combo);
		// GridData gridData = new GridData(SWT.FILL, SWT.CENTER, false, false);
		// formatLabel.setLayoutData(gridData);
		//
		// formatCombo = new Combo(composite2, SWT.READ_ONLY);
		// formatCombo.setItems(getTemplateNames());
		// formatCombo.select(fGenerationSettings.stringFormatTemplateNumber);
		// formatCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
		// false));
		// SWTUtil.setDefaultVisibleItemCount(formatCombo);
		// formatCombo.addSelectionListener(new SelectionAdapter() {
		// public void widgetSelected(SelectionEvent e) {
		// fGenerationSettings.stringFormatTemplateNumber = ((Combo)
		// e.widget).getSelectionIndex();
		// }
		// });
		//
		// final Button formatButton = new Button(composite2, SWT.NONE);
		// formatButton.setText(JavaUIMessages.GenerateToStringDialog_manage_templates_button);
		// setButtonLayoutData(formatButton);
		// formatButton.addSelectionListener(new SelectionAdapter() {
		// public void widgetSelected(SelectionEvent e) {
		// manageTemplatesButtonSelected();
		// }
		// });
		//
		// final Label styleLabel = new Label(composite2, SWT.NONE);
		// styleLabel.setText(JavaUIMessages.GenerateToStringDialog_code_style_combo);
		// gridData = new GridData(SWT.FILL, SWT.CENTER, false, false);
		// styleLabel.setLayoutData(gridData);
		//
		// final Combo styleCombo = new Combo(composite2, SWT.READ_ONLY);
		// styleCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
		// false));
		// styleCombo.setItems(GenerateToStringOperation.getStyleNames());
		// styleCombo.select(Math.min(fGenerationSettings.toStringStyle,
		// styleCombo.getItemCount() - 1));
		// SWTUtil.setDefaultVisibleItemCount(styleCombo);
		// styleCombo.addSelectionListener(new SelectionAdapter() {
		// public void widgetSelected(SelectionEvent e) {
		// changeToStringStyle(((Combo) e.widget).getSelectionIndex());
		// }
		// });
		//
		// styleButton = new Button(composite2, SWT.NONE);
		// styleButton.setText(JavaUIMessages.GenerateToStringDialog_codeStyleConfigureButton);
		// setButtonLayoutData(styleButton);
		// styleButton.addSelectionListener(new SelectionAdapter() {
		// public void widgetSelected(SelectionEvent e) {
		// configureStyleButtonSelected();
		// }
		// });
		//
		// skipNullsButton = new Button(group, SWT.CHECK);
		// skipNullsButton.setText(JavaUIMessages.GenerateToStringDialog_skip_null_button);
		// skipNullsButton.setSelection(fGenerationSettings.skipNulls);
		// skipNullsButton.addSelectionListener(new SelectionAdapter() {
		// public void widgetSelected(SelectionEvent event) {
		// fGenerationSettings.skipNulls = ((Button)
		// event.widget).getSelection();
		// }
		// });
		//
		// final Button arrayButton = new Button(group, SWT.CHECK);
		// arrayButton.setText(JavaUIMessages.GenerateToStringDialog_ignore_default_button);
		// arrayButton.setSelection(fGenerationSettings.customArrayToString);
		// arrayButton.addSelectionListener(new SelectionAdapter() {
		// public void widgetSelected(SelectionEvent e) {
		// fGenerationSettings.customArrayToString = ((Button)
		// e.widget).getSelection();
		// }
		// });
		//
		// Composite limitRow = new Composite(group, SWT.NONE);
		// RowLayout rowLayout = new RowLayout();
		// rowLayout.center = true;
		// rowLayout.marginLeft = rowLayout.marginRight = rowLayout.marginTop =
		// rowLayout.marginBottom = 0;
		// limitRow.setLayout(rowLayout);
		//
		// final Button limitButton = new Button(limitRow, SWT.CHECK);
		// limitButton.setText(JavaUIMessages.GenerateToStringDialog_limit_elements_button);
		// limitButton.setSelection(fGenerationSettings.limitElements);
		// limitButton.addSelectionListener(new SelectionAdapter() {
		// public void widgetSelected(SelectionEvent e) {
		// fGenerationSettings.limitElements = ((Button)
		// e.widget).getSelection();
		// }
		// });
		//
		// final Spinner limitSpinner = new Spinner(limitRow, SWT.BORDER);
		// limitSpinner.setMinimum(0);
		// limitSpinner.setSelection(fGenerationSettings.limitValue);
		// limitSpinner.addSelectionListener(new SelectionAdapter() {
		// public void widgetSelected(SelectionEvent e) {
		// fGenerationSettings.limitValue = ((Spinner) e.widget).getSelection();
		// }
		// });
		//
		// // invoked to change initial enable state of controls
		// changeToStringStyle(styleCombo.getSelectionIndex());

		return composite;
	}

	private void manageTemplatesButtonSelected() {
		// TODO delete
	}

	private void configureStyleButtonSelected() {
		// TODO delete
	}

	// private void changeToStringStyle(int style) {
	// fGenerationSettings.toStringStyle = style;
	// skipNullsButton.setEnabled(style !=
	// GenerateToStringOperation.STRING_FORMAT);
	// boolean enableFormat = (style !=
	// GenerateToStringOperation.CUSTOM_BUILDER);
	// formatLabel.setEnabled(enableFormat);
	// formatCombo.setEnabled(enableFormat);
	// styleButton.setEnabled(style ==
	// GenerateToStringOperation.CUSTOM_BUILDER);
	// updateOKStatus();
	// }

}
